/**
 * * Program : GTravelling
 * Written by : Le Xuan Ninh 
 * Email : xuanninh.it@gmail.com
 * Created date: 06/03/2012 
 * Modified by: 
 * Modified date:
 * Version: 1.0
 * Description:
 */
package com.android.gtravelling;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.android.dto.ProvincesDTO;

public class VietTravel extends Activity {

	private ListView mainListView;
	private ArrayList<ProvincesDTO> listProvinces;
	private ArrayAdapter<ProvincesDTO> listAdapter;
	private WebserviceCaller WebCaller;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_province);

		mainListView = (ListView) findViewById(R.id.lv_province);
		WebCaller = new WebserviceCaller();
		listProvinces = WebCaller.selectAllProvinces();
		listAdapter = new ProvincesDTOArrayAdapter(this, listProvinces);
		mainListView.setAdapter(listAdapter);

	}

	public void displayAMessage(String msg) {

		AlertDialog alertDialog;
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Message");
		alertDialog.setMessage(msg);

		alertDialog.show();
	}

	private class ProvincesDTOViewHolder {
		private ImageView imgView;

		public ImageView getImgView() {
			return imgView;
		}

		public void setImgView(ImageView imgView) {
			this.imgView = imgView;
		}

		private TextView textView;
		private TextView textView1;

		public ProvincesDTOViewHolder() {
		}

		public ProvincesDTOViewHolder(TextView textView) {
			this.textView = textView;
		}

		public ProvincesDTOViewHolder(TextView textView, TextView textView1) {
			this.textView = textView;
			this.textView1 = textView1;

			this.textView.setOnLongClickListener(longLst);
		}

		public ProvincesDTOViewHolder(ImageView imgView, TextView textView,
				TextView textView1) {
			this.imgView = imgView;
			this.textView = textView;
			this.textView1 = textView1;

			this.textView.setOnLongClickListener(longLst);
		}

		public TextView getTextView() {
			return textView;
		}

		public TextView getTextView1() {
			return this.textView1;
		}

		public void setTextView(TextView textView) {
			this.textView = textView;
		}

		OnLongClickListener longLst = new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(v.getContext(), EditActivity.class);
				i.putExtra(Intent.EXTRA_TEXT, textView.getText().toString());
				startActivity(i);
				return false;
			}
		};

	}

	private class ProvincesDTOArrayAdapter extends ArrayAdapter<ProvincesDTO> {

		private LayoutInflater inflater;

		public ProvincesDTOArrayAdapter(Context context,
				List<ProvincesDTO> planetList) {
			super(context, R.layout.row_province, R.id.tv_province_title,
					planetList);
			// Cache the LayoutInflate to avoid asking for a new one each time.
			inflater = LayoutInflater.from(context);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ProvincesDTO prov = (ProvincesDTO) this.getItem(position);

			// The child views in each row.
			ImageView imgView;
			TextView textView;
			TextView textView1;
			// Create a new row view
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.row_province, null);
				// Find the child views.
				imgView=(ImageView)convertView.findViewById(R.id.imgRowContent);
				textView = (TextView) convertView
						.findViewById(R.id.tv_province_title);

				textView1 = (TextView) convertView
						.findViewById(R.id.tv_province_Descrip);

				// Optimization: Tag the row with it's child views, so we don't
				// have to
				// call findViewById() later when we reuse the row.
				convertView.setTag(new ProvincesDTOViewHolder(imgView,textView,
						textView1));
			}
			// Reuse existing row view
			else {
				// Because we use a ViewHolder, we avoid having to call
				// findViewById().
				ProvincesDTOViewHolder viewHolder = (ProvincesDTOViewHolder) convertView
						.getTag();
				imgView=viewHolder.getImgView();
				textView = viewHolder.getTextView();
				textView1 = viewHolder.getTextView1();
			}

			// Display planet data
			imgView.setImageResource(R.drawable.cui_bap);
			textView.setText(prov.getName());
			textView1.setText(prov.getDescription());

			return convertView;
		}

	}
}
